import Toast from '../../component/vant/toast/toast';
const util = require('../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    my_course: [],
    domain: app.api.xh.downloadDomain,
    teacherTagStyle: util.teacherTagStyles,
    loaded: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var logging = wx.getStorageSync('_logining');

    if (!logging) {
      app.toAuth('/pages/study/study');
    } else {
      this.getCourseList();
    }
  },
  frmClick(e) {
    app.saveFormId(e.detail.formId);
  },
  onChange(e) {
    this.setData({
      currentTab: e.detail.index
    });
    app.goTop();
  },
  //  我的课程
  getCourseList: function() {
    var page = this;
    app.sendRequest({
      url: app.api.student.myself,
      data: {
        type: 2
      },
      success: function(res) {
        if (res.data != undefined && res.data != '' && res.data.list) {
          page.setData({
            my_course: res.data.list
          });
          page.initData();
        }
      }
    });
  },
  initData() {
    let page = this,
      my_course = page.data.my_course,
      all_course = [],
      charge_course = [],
      free_course = [],
      completed_course = [];
    my_course.map(course => {
      let enter_time = course.enter_time.substr(0, 10),
        diff_day = util.dateDiff(enter_time);
      course.enter_time = enter_time;
      course.learned_day = diff_day;
      all_course.push(course);
      // 付费课程
      if (course.now_cost > 0) {
        charge_course.push(course);
      } else if (course.now_cost == 0) {
        //   免费课程
        free_course.push(course);
      }
      course.progress = course.progress ? Math.round(course.progress) : 0;
      //已完成课程
      if (course.progress == 100) {
        completed_course.push(course);
      }
    });
    page.setData({
      loaded: 1,
      all_course: all_course,
      charge_course: charge_course,
      free_course: free_course,
      completed_course: completed_course
    });
  },
  chooseCourse() {
    wx.navigateTo({
      url: '/pages/course_filter/course_filter'
    });
  },
  toCourse: function(e) {
    var cid = e.currentTarget.dataset.id;
    var curriculum_name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url:
        '/pages/course_detail/course_detail?curriculum_id=' +
        cid +
        '&curriculum_name=' +
        curriculum_name
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var logging = wx.getStorageSync('_logining');

    if (logging) {
      this.getCourseList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getCourseList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
